//
//  SugViewController.m
//  xiangmu
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 rzt. All rights reserved.
//

#import "SugViewController.h"


@interface SugViewController ()<UITextViewDelegate,TZImagePickerControllerDelegate>
{
    NSMutableArray *_selectedPhotos;
}

@end

@implementation SugViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"问题反馈";
    _selectedPhotos = [NSMutableArray array];
}


-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.textLabel.hidden=YES;
}

-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length==0) {
        self.textLabel.hidden=NO;
    }else{
        self.textLabel.hidden=YES;
    }
}
- (IBAction)tijiaoBtnTap:(id)sender {
    if (self.textview.text.length>0&&self.lianxiTextField.text.length>0) {
        MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.completionBlock = ^(){
            [self.view makeToast:@"提交成功，感谢您的建议~" duration:2.0 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
        };
        [HUD hideAnimated:YES afterDelay:1.0];
    }else if(self.textview.text.length==0){
        [self.view makeToast:@"请输入您的建议~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else{
        [self valiMobile:self.lianxiTextField.text];
    }
}

- (IBAction)addBtnTap:(id)sender {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imagePickerVc.allowPickingVideo=NO;
    imagePickerVc.allowTakeVideo=NO;
    imagePickerVc.naviBgColor=[UIColor redColor];
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        self->_selectedPhotos = [NSMutableArray arrayWithArray:photos];
        [self.addBtn setBackgroundImage:self->_selectedPhotos[0] forState:UIControlStateNormal];
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

-(NSString *)valiMobile:(NSString *)mobile {
    if (mobile.length != 11) {
        [self.view makeToast:@"手机号只能是11位"
                    duration:2.0
                    position:CSToastPositionCenter];
        
    } else {
            NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(17[0-9])|(18[2-4,7-8]))\\d{8}$";
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(17[0-9])|(18[5,6]))\\d{8}$";
        NSString *CT_NUM = @"^((133)|(153)|(17[0-9])|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        if (isMatch1 || isMatch2 || isMatch3) {
            return nil;
        } else {
            [self.view makeToast:@"请输入正确的手机号"
                        duration:2.0
                        position:CSToastPositionCenter];
            }
    }
    return nil;
}
@end
